//
//  VRPushViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/13.
//

import UIKit
import ZLPhotoBrowser

class VRPushViewController: VRBaseViewController, SAAnimationProtocol {
    
    private let scrollView = UIScrollView()
    private let containerView = UIView()
    private let bgImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let backButton          = c_make_ex(UIButton.self)
        .image("return").end()
    private let picButton         = c_make_ex(UIButton.self)
        .image("Home_pic").corner(15).end()
    private let nameTextField         = c_make_ex(UITextField.self)
        .placeholder("Add a title to chat").font(16.font_medium)
        .color(.white).end()
    private let nameLineView        = c_make_ex(UIView.self)
        .bgColor(.white.alpha(0.1)).end()
    private let languageLabel = c_make_ex(UILabel.self)
        .text("language").color(.white).font(18.font_bold).end()
    private let languageValueLabel = c_make_ex(UILabel.self)
        .alignment(.right).color(.white).font(16.font_medium).end()
    private let langRightImg = UIIconButton(style: .trailing(itemSpace: 5))
    private let selLangButton = c_make_ex(UIButton.self).end()
    private let selTagLabel = c_make_ex(UILabel.self)
        .text("Select tag").color(.white).font(18.font_bold).end()
    private let tagRightImg = c_make_ex(UIImageView.self)
        .image("Discover_go").end()
    private let selTagButton = c_make_ex(UIButton.self).end()
    private let oneTagButton = c_make_ex(UIButton.self)
        .color("#FFFC00").font(14.font_medium).corner(15)
        .border("#FFFC00",width: 1).text("#music").end()
    private let twoTagButton = c_make_ex(UIButton.self)
        .color("#FFFC00").font(14.font_medium).corner(15)
        .border("#FFFC00",width: 1).text("#music").end()
    private let threeTagButton = c_make_ex(UIButton.self)
        .color("#FFFC00").font(14.font_medium).corner(15)
        .border("#FFFC00",width: 1).text("#music").end()
    private let permossionLabel = c_make_ex(UILabel.self)
        .text("Permission").color(.white).font(18.font_bold).end()
    private let permossionButton = c_make_ex(UIButton.self)
        .image("Home_switch_on",state: .selected)
        .image("Home_switch_off").end()
    private let bgLabel = c_make_ex(UILabel.self)
        .text("Back ground").color(.white).font(18.font_bold).end()
    private let oneBgButton = c_make_ex(UIButton.self)
        .color(.black,state: .selected).color("#FFFC00".toColor.alpha(0.6))
        .font(14.font_medium).corner(15)
        .bgImage(UIImage.colorToImage(color: "#FFFC00".toColor),state: .selected)
        .bgImage(UIImage.colorToImage(color: "#FFFC00".toColor.alpha(0.1))).text("Quite").end()
    private let twoBgButton = c_make_ex(UIButton.self)
        .color(.black,state: .selected).color("#FFFC00".toColor.alpha(0.6))
        .font(14.font_medium).corner(15)
        .bgImage(UIImage.colorToImage(color: "#FFFC00".toColor),state: .selected)
        .bgImage(UIImage.colorToImage(color: "#FFFC00".toColor.alpha(0.1))).text("Lively").end()
    private let threeBgButton = c_make_ex(UIButton.self)
        .color(.black,state: .selected).color("#FFFC00".toColor.alpha(0.6))
        .font(14.font_medium).corner(15)
        .bgImage(UIImage.colorToImage(color: "#FFFC00".toColor),state: .selected)
        .bgImage(UIImage.colorToImage(color: "#FFFC00".toColor.alpha(0.1))).text("Landscape").end()
    private let addButton = c_make_ex(UIButton.self)
        .image("Live_add").end()
    private let singleButton = c_make_ex(UIButton.self)
        .text("Single").color(.white.alpha(0.4)).font(16.font_semiBold)
        .color(.white,state: .selected).end()
    private let multiButton = c_make_ex(UIButton.self)
        .text("Multi-Person").color(.white.alpha(0.4)).font(16.font_semiBold)
        .color(.white,state: .selected).end()
    private let singleLineView        = c_make_ex(UIView.self)
        .bgColor(.white).end()
    private let multiLineView        = c_make_ex(UIView.self)
        .bgColor(.white).end()
    private lazy var goButton = c_make_ex(UIButton.self)
        .color(.black).font(18.font_bold).corner(25)
        .text("Go live").bgColor("#FFFC00").end()
    
    private var tagString = ""
    private var picUrl = ""
    private var bgUrl = ""
    private var bgImage: UIImage?
    private var languageStr = ""
    private var multiType = "0"//0 single 1 multi
    
    init() {
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .fullScreen
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        
        view.subviews{
            bgImageView
//            scrollView.subviews{
//                containerView.subviews{
                    picButton
                    nameTextField
                    nameLineView
                    languageLabel
                    languageValueLabel
                    langRightImg
                    selLangButton
                    selTagLabel
                    tagRightImg
                    selTagButton
                    oneTagButton
                    twoTagButton
                    threeTagButton
                    permossionLabel
                    permossionButton
                    bgLabel
                    oneBgButton
                    twoBgButton
                    threeBgButton
                    addButton
                    singleButton
                    multiButton
                    singleLineView
                    multiLineView
                    goButton
//                }
//            }
            backButton
        }
        
//        scrollView.leading(0).trailing(0).bottom(0).top(0)
//        scrollView.contentInsetAdjustmentBehavior = .never
//        containerView.fillContainer().width(.screenWidth)
        bgImageView.fillContainer()
        backButton.top(.statusHeight + 2).leading(5).size(40)
        picButton.size(120).top(.navi_height + 12).centerHorizontally()
        nameTextField.leading(15).trailing(15).height(50)
        nameTextField.Top == picButton.Bottom + 10
        nameLineView.leading(15).trailing(15).height(2)
        nameLineView.Bottom == nameTextField.Bottom
        languageLabel.leading(15).height(50)
        languageLabel.Top == nameLineView.Bottom + 15
        langRightImg.trailing(15).height(50)
        langRightImg.CenterY == languageLabel.CenterY
        languageValueLabel.CenterY == langRightImg.CenterY
        languageValueLabel.Trailing == langRightImg.Leading - 5
        selLangButton.leading(0).trailing(0)
        selLangButton.Top == languageLabel.Top
        selLangButton.Bottom == languageLabel.Bottom
        selTagLabel.leading(15).height(50)
        selTagLabel.Top == languageLabel.Bottom + 10
        tagRightImg.trailing(15).size(22)
        tagRightImg.CenterY == selTagLabel.CenterY
        selTagButton.leading(0).trailing(0)
        selTagButton.Top == selTagLabel.Top
        selTagButton.Bottom == selTagLabel.Bottom
        oneTagButton.leading(15).height(30)
        oneTagButton.Top == selTagLabel.Bottom
        twoTagButton.height(30)
        twoTagButton.CenterY == oneTagButton.CenterY
        twoTagButton.Leading == oneTagButton.Trailing + 12
        threeTagButton.height(30)
        threeTagButton.CenterY == twoTagButton.CenterY
        threeTagButton.Leading == twoTagButton.Trailing + 12
        addButton.size(31)
        addButton.CenterY == threeBgButton.CenterY
        addButton.Leading == threeBgButton.Trailing + 12
        permossionLabel.leading(15).height(50)
        permossionLabel.Top == selTagLabel.Bottom
        permossionButton.trailing(15).height(31).width(51)
        permossionButton.CenterY == permossionLabel.CenterY
        bgLabel.leading(15).height(50)
        bgLabel.Top == permossionLabel.Bottom + 10
        oneBgButton.leading(15).width(70).height(30)
        oneBgButton.Top == bgLabel.Bottom
        twoBgButton.leading(100).width(75).height(30)
        twoBgButton.CenterY == oneBgButton.CenterY
        threeBgButton.leading(190).width(105).height(30)
        threeBgButton.CenterY == twoBgButton.CenterY
        singleButton.width(140).height(40).leading(40)
        singleButton.Top == oneBgButton.Bottom + 35
        multiButton.width(140).height(40).trailing(60)
        multiButton.Top == oneBgButton.Bottom + 35
        singleLineView.height(3).width(15)
        singleLineView.Bottom == singleButton.Bottom
        singleLineView.CenterX == singleButton.CenterX
        multiLineView.height(3).width(15)
        multiLineView.Bottom == multiButton.Bottom
        multiLineView.CenterX == multiButton.CenterX
        goButton.width(310).height(50).centerHorizontally()
        goButton.Top == singleLineView.Bottom + 35

        scrollView.isUserInteractionEnabled = true
        containerView.isUserInteractionEnabled = true
//        bgImageView.isUserInteractionEnabled = true
        nameTextField.placehold(.white.alpha(0.4), font: 16.font_medium)
        langRightImg.style { v in
            v.setImage("Discover_go".toImage, for: .normal)
            v.setTitleColor(.white, for: .normal)
            v.titleLabel?.font = 16.font_medium
            v.isUserInteractionEnabled = false
        }
        oneTagButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
        twoTagButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
        threeTagButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
        oneTagButton.isHidden = true
        twoTagButton.isHidden = true
        threeTagButton.isHidden = true
        oneBgButton.isSelected = false
        twoBgButton.isSelected = false
        threeBgButton.isSelected = false
        singleButton.isSelected = true
        multiButton.isSelected = false
        singleLineView.isHidden = false
        multiLineView.isHidden = true
        
        picButton.addTarget(self, action: #selector(clickPicButton), for: .touchUpInside)
        backButton.addTarget(self, action: #selector(clickDismissButton), for: .touchUpInside)
        singleButton.addTarget(self, action: #selector(clickSingleButton), for: .touchUpInside)
        multiButton.addTarget(self, action: #selector(clickMultiButton), for: .touchUpInside)
        oneBgButton.addTarget(self, action: #selector(clickQuiteButton), for: .touchUpInside)
        twoBgButton.addTarget(self, action: #selector(clickLivelyButton), for: .touchUpInside)
        threeBgButton.addTarget(self, action: #selector(clickLandscapeButton), for: .touchUpInside)
        addButton.addTarget(self, action: #selector(clickAddButton), for: .touchUpInside)
        permossionButton.addTarget(self, action: #selector(clickPermButton), for: .touchUpInside)
        selLangButton.addTarget(self, action: #selector(clickLanguageButton), for: .touchUpInside)
        selTagButton.addTarget(self, action: #selector(clickTagButton), for: .touchUpInside)
        goButton.addTarget(self, action: #selector(clickGoButton), for: .touchUpInside)
    }
    
}


//MARK: action
extension VRPushViewController {
    
    @objc private func clickGoButton() {
        
        if picUrl.count == 0 {
            "Please upload a cover image".toast_error
            return
        }
        guard let name = nameTextField.text,name.count > 0 else {
            "Please add a title to chat".toast_error
            return
        }
        if languageStr.count == 0 {
            "Please select a language".toast_error
            return
        }
        if tagString.count == 0 {
            "Please select at least one tag".toast_error
            return
        }

        if let bgImg = bgImage, bgUrl.count == 0 {
            RT.uploadCoverImage(file: (self.imageToBase64String(image: bgImg))!) { dict in
                self.bgUrl = dict.url!
            }
        }
        if bgUrl.count == 0 {
            "Please select a backgound".toast_error
        }
        
        var permossionStr = "0"
        if permossionButton.isSelected {
            permossionStr = "1"
        }
        let dict = ["icon": picUrl,"houseName": name,"info5": languageStr,"info": tagString, "info6": bgUrl, "info7": permossionStr,  "userId": USEINFOMODEL.id ?? "", "info1": USEINFOMODEL.headImage ?? "", "info2": USEINFOMODEL.name ?? "", "info3": Date().toString(.yearMonthDay), "price": "0", "info9": USEINFOMODEL.gender ?? "0","info0": "VoiceRoom___*Live"]
        RT.createHouse(paraInfo: dict) { s in
            "Success!".toast_success
            self.dismiss(animated: true)
        }
    }
    
    @objc private func clickPicButton() {
        
        ZLPhotoConfiguration.default()
            .allowSelectVideo(false)
        let ps = ZLPhotoPreviewSheet()
        ps.selectImageBlock = { [weak self] results, isOriginal in
            ToastTool.show()
            let image = results.first?.image
            self?.picButton.setImage(image, for: .normal)
            RT.uploadCoverImage(file: (self?.imageToBase64String(image: image!))!) { [weak self] (dict) in
                ToastTool.dismiss()
                self?.picUrl = dict.url!
            }
        }
        ps.showPhotoLibrary(sender: self)
    }
    
    @objc private func clickLanguageButton(){
        
        let languageV = VRLiveLanguageView()
        languageV.didChooseCompletion = { [weak self] selStr in
            self?.languageStr = selStr
            self?.languageValueLabel.text = selStr
        }
        saAnimate.animation(show: languageV)
    }
    
    @objc private func clickPermButton(){
        
        permossionButton.isSelected = !permossionButton.isSelected
    }
    
    @objc private func clickQuiteButton(){
        
        if !oneBgButton.isSelected {
            
            oneBgButton.isSelected = true
            twoBgButton.isSelected = false
            threeBgButton.isSelected = false
            bgImageView.image = "Live_quiteBg".toImage
            bgImage = "Live_quiteBg".toImage
        }
    }
    
    @objc private func clickLivelyButton(){
        
        if !twoBgButton.isSelected {
            
            oneBgButton.isSelected = false
            twoBgButton.isSelected = true
            threeBgButton.isSelected = false
            bgImageView.image = "Live_livelyBg".toImage
            bgImage = "Live_quiteBg".toImage
        }
    }
    
    @objc private func clickLandscapeButton(){
        
        if !threeBgButton.isSelected {
            
            oneBgButton.isSelected = false
            twoBgButton.isSelected = false
            threeBgButton.isSelected = true
            bgImageView.image = "Live_landscapeBg".toImage
            bgImage = "Live_quiteBg".toImage
        }
    }
    
    @objc private func clickAddButton(){
        
        ZLPhotoConfiguration.default()
            .allowSelectVideo(false)
        let ps = ZLPhotoPreviewSheet()
        ps.selectImageBlock = { [weak self] results, isOriginal in
            ToastTool.show()
            let image = results.first?.image
            self?.bgImageView.image = image
            self?.oneBgButton.isSelected = false
            self?.twoBgButton.isSelected = false
            self?.threeBgButton.isSelected = false
            RT.uploadCoverImage(file: (self?.imageToBase64String(image: image!))!) { [weak self] (dict) in
                ToastTool.dismiss()
                self?.bgUrl = dict.url!
            }
        }
        ps.showPhotoLibrary(sender: self)
    }
    
    @objc private func clickSingleButton(){
        
        if !singleButton.isSelected {
            
            singleButton.isSelected = true
            singleLineView.isHidden = false
            multiButton.isSelected = false
            multiLineView.isHidden = true
            multiType = "0"
        }
    }
    
    @objc private func clickMultiButton(){
        
        if !multiButton.isSelected {
            
            singleButton.isSelected = false
            singleLineView.isHidden = true
            multiButton.isSelected = true
            multiLineView.isHidden = false
            multiType = "1"
        }
    }
    
    @objc private func clickTagButton(){
        
        
        let deleteV = VRSelectTagView()
        deleteV.tagStringsBlock = { [weak self] tagStr in
            self?.tagString = tagStr
            if tagStr.count > 0 {
                self?.permossionLabel.topConstraint?.constant = 50
                let arr = tagStr.components(separatedBy: ",")
                if arr.count == 1 {
                    self?.oneTagButton.isHidden = false
                    self?.twoTagButton.isHidden = true
                    self?.threeTagButton.isHidden = true
                    
                    self?.oneTagButton.setTitle(arr.first, for: .normal)
                }else if arr.count == 2 {
                    self?.oneTagButton.isHidden = false
                    self?.twoTagButton.isHidden = false
                    self?.threeTagButton.isHidden = true
                    
                    self?.oneTagButton.setTitle(arr.first, for: .normal)
                    self?.twoTagButton.setTitle(arr[1], for: .normal)
                }else{
                    self?.oneTagButton.isHidden = false
                    self?.twoTagButton.isHidden = false
                    self?.threeTagButton.isHidden = false
                    
                    self?.oneTagButton.setTitle(arr.first, for: .normal)
                    self?.twoTagButton.setTitle(arr[1], for: .normal)
                    self?.threeTagButton.setTitle(arr.last, for: .normal)
                }
            }else{
                self?.permossionLabel.topConstraint?.constant = 0
            }
//            self.updatePostButtonState()
        }
        saAnimate.animation(show: deleteV)
    }
    
    @objc private func clickDismissButton(){
        self.dismiss(animated: true)
    }
}
